package com.viknix.threadpool.manager.cluster.core;

import com.viknix.threadpool.manager.server.dependence.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: Dongqi
 * @Date: 2021/11/11 9:44
 * @Version 1.0
 * @Description: task.
 */
@Slf4j
@SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule")
public abstract class Task implements Runnable {

    protected volatile boolean shutdown = false;

    @Override
    public void run() {
        if (shutdown) {
            return;
        }
        try {
            executeBody();
        } catch (Throwable t) {
            log.error(">>> [😒 this task execute has error] : {}", ExceptionUtil.getStackTrace(t));
        } finally {
            if (!shutdown) {
                after();
            }
        }
    }

    /**
     * Task executive.
     */
    protected abstract void executeBody();

    /**
     * after executeBody should do.
     */
    protected void after() {

    }

    public void shutdown() {
        shutdown = true;
    }

}
